package controller;

import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import entity.User;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.*;
import vo.AjaxResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

/**
 * Author：汤小洋
 * Date：2023-09-21 13:37
 * Description：<描述>
 */
@RestController
public class ParamController {

    /**
     * JavaEE组件
     */
    @SneakyThrows
    @RequestMapping("/param1")
    public void param1(HttpServletRequest req, HttpServletResponse res, HttpSession session) {
        System.out.println(req.getParameter("name"));

        System.out.println(session.getId());

        res.setContentType("text/html;charset=UTF-8");
        res.getWriter().print("<h2>成功</h2>");
    }

    /**
     * String、基本类型及包装类
     */
    @RequestMapping("/param2")
    public AjaxResult param2(@RequestParam("name") String name,@RequestParam(required = false, defaultValue = "18") int age, Double height){
        System.out.println(name);
        System.out.println(age);
        System.out.println(height);
        return AjaxResult.success();
    }

    @RequestMapping("/param3/{id}/{name}")  //  /param3/6
    public AjaxResult param3(@PathVariable("id") Integer id,@PathVariable String name){
        System.out.println(id);
        System.out.println(name);
        return AjaxResult.success();
    }

    @RequestMapping("/param4")
    public AjaxResult param4(@RequestHeader("User-Agent") String userAgent){
        // System.out.println(userAgent);
        UserAgent agent = UserAgentUtil.parse(userAgent);
        System.out.println(agent.getOs().getName());
        System.out.println(agent.getBrowser().getName());
        return AjaxResult.success();
    }

    @RequestMapping("/param5")
    public AjaxResult param5(@CookieValue("address") String address){
        System.out.println(address);
        return AjaxResult.success();
    }

    @RequestMapping("/param6")
    // public AjaxResult param6(@RequestBody String str){
    // public AjaxResult param6(@RequestBody User user){
    // public AjaxResult param6(@RequestBody Integer[] ids){
    // public AjaxResult param6(@RequestBody List<Integer> ids){
    // public AjaxResult param6(@RequestBody List<User> users){
    public AjaxResult param6(@RequestBody String str){
        // System.out.println(str);
        // System.out.println(user);
        // System.out.println(Arrays.toString(ids));
        // System.out.println(ids);
        // System.out.println(users);
        System.out.println(str);
        JSONObject obj = JSON.parseObject(str);
        System.out.println(obj.get("uid"));

        return AjaxResult.success();
    }

    /**
     * 自定义对象
     */
    @RequestMapping("/param7")
    public  AjaxResult param7(@ModelAttribute User user){
        System.out.println(user);
        return AjaxResult.success(user);
    }

}
